package com.tangruojie.controller;

import jakarta.servlet.ServletInputStream;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.core.io.Resource;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.URI;
import java.util.Arrays;
import java.util.Locale;

@RestController
@RequestMapping("/http")
public class HttpController {

    @GetMapping("/HttpServletRequest")
    public String httpServletRequest(@RequestParam("name") String name, HttpServletRequest request) {
        String contentType = request.getContentType();
        System.out.println("contentType: " + contentType);
        System.out.println("method: " + request.getMethod());
        System.out.println("requestURI: " + request.getRequestURI());
        System.out.println("header: " + request.getHeader("Accept"));
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println("cookie: " + cookie.getName() + ": " + cookie.getValue());
            }
        } else {
            System.out.println("No cookies received.");
        }
        HttpSession session = request.getSession();
        System.out.println("session ID: " + session.getId());
        Object nameInSession = session.getAttribute("name");
        System.out.println("session attribute 'name': " + nameInSession);
        return "HttpServletRequest OK";
    }

    @GetMapping("/HttpServletResponse")
    public String httpServletResponse(@RequestParam("name") String name, HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        response.setStatus(200);
        response.setHeader("name", name);
        response.setContentType("application/json");
        Cookie cookie = new Cookie("name", name);
        cookie.setMaxAge(3600);
        response.addCookie(cookie);
        response.setDateHeader("date", System.currentTimeMillis());
        response.setLocale(Locale.CHINA);
        response.setContentType("application/json");
        return "HttpServletResponse OK";
    }

    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) throws IOException {
        System.out.println("文件大小：" + file.getSize());
        System.out.println("文件名：" + file.getName());
        byte[] bytes = file.getBytes();
        System.out.println("文件字节：" + Arrays.toString(bytes));
        String contentType = file.getContentType();
        System.out.println("文件类型：" + contentType);
        InputStream inputStream = file.getInputStream();
        System.out.println("文件输入流：" + inputStream);
        Resource resource = file.getResource();
        System.out.println("文件资源：" + resource);
        return "上传文件名：" + file.getOriginalFilename();
    }

    @GetMapping("/httpEntity")
    public String httpEntity(HttpEntity<String> httpEntity) {
        HttpHeaders headers = httpEntity.getHeaders();
        headers.forEach((key, value) -> {
            System.out.println(key + ": " + value);
        });
        System.out.println(headers);
        String body = httpEntity.getBody();
        System.out.println(body);
        boolean b = httpEntity.hasBody();
        System.out.println(b);
        return "HttpEntity OK";
    }

    @GetMapping("/requestEntity")
    public String requestEntity(RequestEntity<String> requestEntity) {
        HttpMethod method = requestEntity.getMethod();
        System.out.println(method);
        URI url = requestEntity.getUrl();
        System.out.println(url);
        Type type = requestEntity.getType();
        System.out.println(type);
        HttpHeaders headers = requestEntity.getHeaders();
        System.out.println(headers);
        headers.forEach((key, value) -> {
            System.out.println(key + ": " + value);
        });
        String body = requestEntity.getBody();
        System.out.println(body);
        return "RequestEntity OK";
    }

    @GetMapping("/responseEntity")
    public ResponseEntity<String> responseEntity() {
        ResponseEntity<String> responseEntity = new ResponseEntity<>("唐若洁", HttpStatus.OK);
        HttpHeaders headers = responseEntity.getHeaders();
        headers.forEach((key, value) -> {
            System.out.println(key + ": " + value);
        });
        System.out.println(headers);
        String body = responseEntity.getBody();
        System.out.println(body);
        return responseEntity;
    }

    @GetMapping("/request")
    public String request(HttpServletRequest request) throws IOException {
        String requestURI = request.getRequestURI();
        System.out.println("requestURI: " + requestURI);
        String authType = request.getAuthType();
        System.out.println("authType: " + authType);
        String contentType = request.getContentType();
        System.out.println("contentType: " + contentType);
//        ServletInputStream inputStream = request.getInputStream();
        String remoteHost = request.getRemoteHost();
        System.out.println("remoteHost: " + remoteHost);
        int remotePort = request.getRemotePort();
        System.out.println("remotePort: " + remotePort);
        int localPort = request.getLocalPort();
        System.out.println("localPort: " + localPort);
        int serverPort = request.getServerPort();
        System.out.println("serverPort: " + serverPort);
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println("cookie: " + cookie.getName() + ": " + cookie.getValue());
            }
        } else {
            System.out.println("No cookies received.");
        }
        HttpSession session = request.getSession();
        session.setAttribute("wangchang", "wangchang");
        System.out.println("session ID: " + session.getId());
        return "request OK";
    }

    @GetMapping("/response")
    public String request(HttpServletResponse response) throws IOException {
//        ServletOutputStream outputStream = response.getOutputStream();
        response.addCookie(new Cookie("name1", "tangrujie1"));
        response.addCookie(new Cookie("name2", "tangrujie2"));
        response.addHeader("name1", "tangrujie");
        return "response OK";
    }

}
